import React, { useEffect, useState } from 'react'
import { View,Image, Text, Input } from '@tarojs/components'
import Taro, { useDidShow } from '@tarojs/taro'

import CitySelect from "@/components/city-select"
import './index.scss'

export default function CityList() {
  const [activeCity,setactiveCity] = useState(null)
  const [hotCity,sethotCity] = useState([])
  const [obtainCitys,setobtainCitys] = useState([])
  useEffect(()=>{
    _initData()
    const loca = Taro.getStorageSync('position')
    if(loca && loca.name){
      setactiveCity(loca || {})
    }
  },[])
  function handleSelect(item){
    if(Taro.getCurrentInstance().router?.params?.type == 'back'){
      Taro.setStorageSync('selectCity',item)
      Taro.navigateBack()
      return;
    }
    Taro.setStorageSync('cityInfo',{
      latitude:Taro.getStorageSync('position')?.latitude,
      longitude:Taro.getStorageSync('position')?.longitude,
      city:Taro.getStorageSync('position').city,
      ...item
    })
    Taro.reLaunch({
      url:'/pages/index/index'
    })
  }

  ////       network
  function _initData(){
    Taro.showLoading({title:'加载中...'})
    Taro.$get('/act/cities/embedded','','1').then(res=>{
      if(res.code == '1'){
        setobtainCitys(res.content || [])
      }
    })
    Taro.$get('/act/cities/hot').then(res=>{
      if(res.code == '1'){
        sethotCity(res.content || [])
      }
    })
  }
  return (
    <View className="city-list">
      <CitySelect
          cityClick={handleSelect}
          formatName={'name'}
          activeCity={activeCity}
          hotCity={hotCity}
          obtainCitys={obtainCitys}
          isSearch={false}
      ></CitySelect>
    </View>
  )
}
